#include "deal.h"
#include "print.h"

void dealCallBack(MqttClient& client, const char* topic, const char* data, unsigned int length) {
    if (length <= 0) {
        return;
    }

    String key;
    unsigned int pos = 0;
    if (*(data + (pos++)) == '[') {
        char ch = 0;
        while (pos < length) {
            ch = *(data + (pos++));
            if (ch != ']') {
                key += ch;
            } else {
                break;
            }
        }
    }

    if (pos >= length) {
        println("receive data length error, topic: ", topic, ", data: ", data);
        return;
    }

    if (key.length() <= 0) {
        println("receive data key error, topic: ", topic, ", data: ", data);
        return;
    }

    if (key == "on" || key == "ON") {
        client.pinOn(data + pos, length - pos);
    } else if (key == "off" || key == "OFF") {
        client.pinOff(data + pos, length - pos);
    } else if (key == "STATE") {
        client.pinState(data + pos, length - pos);
    }
}